#import "JokeColumnCell.h"
#import "DBManager.h"
#import "NSString+Md5String.h"
#import "FileManager.h"

@implementation JokeColumnCell

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //
    }
    return self;
}

- (void)dealloc
{
    PP_RELEASE(mainView);
    PP_RELEASE(barView);
    PP_RELEASE(mainBg);
    PP_RELEASE(jokeSummary);
    PP_RELEASE(jokeImg);
    PP_RELEASE(gifTip);
    PP_RELEASE(userName);
    PP_RELEASE(replyNum);
    PP_RELEASE(goodNum);
    PP_RELEASE(badNum);
    PP_RELEASE(replyIcon);
    PP_RELEASE(goodIcon);
    PP_RELEASE(badIcon);
    [super dealloc];
}


-(void)setData:(JokeModel *)pJoke :(NSString *)pDbSort :(BOOL)selected {

    CGRect imgFrame;
    jokeImg.image = nil;
    NSString *imageName = nil;
    if( pJoke.jokeImgUrl.length!=0 ){
        jokeImg.hidden = NO;
        if( [[pJoke.jokeImgUrl lowercaseString] hasSuffix:@"gif"] ){
            NSArray *array = [pJoke.jokeImgUrl componentsSeparatedByString:@"/"];
            imageName = [NSString stringWithFormat:@"small_%@",[array objectAtIndex:[array count]-1]];
            gifTip.hidden = NO;
        }else{
            imageName = [NSString stringWithFormat:@"small_%@",[pJoke.jokeImgUrl md5strLower:pJoke.jokeImgUrl]];
            gifTip.hidden = YES;
        }
        
        NSLog(@"----imageName :%@",imageName);
        NSString *imgFilePath = [SD_CACHES_FOLDER stringByAppendingPathComponent:imageName];
        BOOL jokeImgExsit = [[FileManager sharedInstance]imageExsit:imageName andFolder:SD_CACHES_FOLDER];
        if( jokeImgExsit){
            NSLog(@"1111-.-.--.-.-.--.--.-");
            jokeImg.image = [UIImage imageWithContentsOfFile:imgFilePath];
        }else{
            NSLog(@"2222-.-.--.-.-.--.--.-");
            jokeImg.image = [UIImage imageNamed:@"loading_jokeQuickScan.png"];
        }
        imgFrame = jokeImg.frame;
    }else{
        jokeImg.hidden = YES;
        gifTip.hidden = YES;
        imgFrame = CGRectZero;
    }

//    if( pJoke.jokeImgUrl.length!=0 ){
//        jokeImg.hidden = NO;
//        imgFrame = jokeImg.frame;
//        if( [[pJoke.jokeImgUrl lowercaseString] hasSuffix:@"gif"] ){
//            gifTip.hidden = NO;
//        }else{
//            gifTip.hidden = YES;
//        }
//    }else{
//        jokeImg.hidden = YES;
//        gifTip.hidden = YES;
//        imgFrame = CGRectZero;
//    }
    
    jokeSummary.text = pJoke.jokeText;
    CGSize labelSize = [jokeSummary.text sizeWithFont:[UIFont systemFontOfSize:11] constrainedToSize:CGSizeMake(189, 999) lineBreakMode:UILineBreakModeWordWrap];
    float labelHeight;
    if ( labelSize.height < 20 ) {
        labelHeight = 20;
    } else if ( labelSize.height > 80 && jokeImg.hidden == NO ){
        labelHeight = 80;
    } else if ( labelSize.height > 227 && jokeImg.hidden == YES ){
        labelHeight = 227;
    } else {
        labelHeight = labelSize.height;
    }
    CGRect textFrame = jokeSummary.frame;
    textFrame.size.height = labelHeight;
    jokeSummary.frame = textFrame;
    
    jokeImg.frame = CGRectMake(0,textFrame.origin.y+textFrame.size.height+8,199,140);
    CGRect gifTipFrame = gifTip.frame;
    gifTipFrame.origin.y = jokeImg.frame.origin.y + jokeImg.frame.size.height - gifTipFrame.size.height;
    gifTip.frame = gifTipFrame;
    userName.text = pJoke.userName;
    [self refreshNum:[NSString stringWithFormat:@"%d",pJoke.goodNum] :[NSString stringWithFormat:@"%d",pJoke.badNum] :[NSString stringWithFormat:@"%d",pJoke.replyNum]];
    
    if( pJoke.read ){ // 已读
        jokeSummary.textColor = UIColorFromRGB(0x999999, 1);
        jokeImg.alpha = 0.8;
        replyIcon.selected = NO;
        goodIcon.selected = NO;
        badIcon.selected = NO;
        if( selected ){ // 已选
            mainBg.image = [[UIImage imageNamed:@"joke_column_bg_3.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
        } else { // 未选
            mainBg.image = [[UIImage imageNamed:@"joke_column_bg_2.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
        }
    } else { // 未读
        jokeSummary.textColor = UIColorFromRGB(0x333333, 1);
        jokeImg.alpha = 1;
        replyIcon.selected = YES;
        goodIcon.selected = YES;
        badIcon.selected = YES;
        if( selected ){ // 已选
            mainBg.image = [[UIImage imageNamed:@"joke_column_bg_1.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
        } else { // 未选
            mainBg.image = [[UIImage imageNamed:@"joke_column_bg_0.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
        }
    }
    
    playingId = pJoke.jokeId;
    if( pJoke.jokeImgUrl.length!=0 ){
        JokeModel *tJoke = [pJoke copy];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            [[DBManager sharedInstance] getJokeImgThumb:pDbSort :tJoke];
            dispatch_async(dispatch_get_main_queue(), ^{
                if([playingId isEqual: tJoke.jokeId]){
//                    jokeImg.image = tJoke.jokeImgThumbnail;
                }
                [tJoke release];
            });
        });
    }
}

-(void)refreshNum:(NSString *)pGoodNum :(NSString *)pBadNum :(NSString *)pReplyNum {
    goodNum.text = pGoodNum;
    badNum.text = pBadNum;
    replyNum.text = pReplyNum;
}

@end
